import request from '@/utils/request'

// 单个文件上传
export function uploadFile(file) {
  const formData = new FormData()
  formData.append('file', file)
  
  return request({
    url: '/file/oss/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 批量文件上传（串行）
export function uploadFiles(files) {
  const formData = new FormData()
  files.forEach(file => {
    formData.append('files', file)
  })
  
  return request({
    url: '/file/oss/uploads',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    timeout: 120000 // 2分钟超时
  })
}

// 批量文件上传（并发）
export function uploadFilesConcurrent(files) {
  const formData = new FormData()
  files.forEach(file => {
    formData.append('files', file)
  })
  
  return request({
    url: '/file/oss/uploads-concurrent',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    timeout: 90000 // 90秒超时
  })
}

// 删除文件
export function deleteFile(fileUrl) {
  return request({
    url: '/file/oss/delete',
    method: 'delete',
    params: {
      fileUrl: fileUrl
    }
  })
}

// 检查文件是否存在
export function checkFileExists(fileUrl) {
  return request({
    url: '/file/oss/exists',
    method: 'get',
    params: {
      fileUrl: fileUrl
    }
  })
}

// 获取文件访问URL
export function getFileUrl(fileName) {
  return request({
    url: '/file/oss/url',
    method: 'get',
    params: {
      fileName: fileName
    }
  })
}

// 获取带签名的文件访问URL
export function getSignedUrl(fileName, expireTime = 3600) {
  return request({
    url: '/file/oss/signed-url',
    method: 'get',
    params: {
      fileName: fileName,
      expireTime: expireTime
    }
  })
}

// 批量上传文件并返回拼接路径（专用于数据库存储）
export function uploadFilesForDatabase(files, separator = '|') {
  const formData = new FormData()
  files.forEach(file => {
    formData.append('files', file)
  })
  
  return request({
    url: '/file/oss/uploads-for-database',
    method: 'post',
    data: formData,
    params: {
      separator: separator
    },
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    timeout: 120000 // 2分钟超时
  })
}

// 并发批量上传文件并返回拼接路径（专用于数据库存储 - 高性能版）
export function uploadFilesForDatabaseConcurrent(files, separator = '|') {
  const formData = new FormData()
  files.forEach(file => {
    formData.append('files', file)
  })
  
  return request({
    url: '/file/oss/uploads-for-database-concurrent',
    method: 'post',
    data: formData,
    params: {
      separator: separator
    },
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    timeout: 90000 // 90秒超时
  })
}